const connection = require("../app/database")

class CategoryService {
    async create(name) {
        const statement = `INSERT INTO category (name) VALUE (?);`
        const result = await connection.execute(statement,[name])
        console.log("result",result);
        return result
    }
    async getListCount() {
        const statement = `SELECT COUNT(*) AS totalCount from category;`
        const [result] = await connection.execute(statement)
        return result[0]    
    }
    async getList(pageSize,offset) {
        const statement = `SELECT * from category ORDER BY id desc limit ${pageSize} offset ${offset};;`
        const [result] = await connection.execute(statement)
        return result
    }
    async edit(id,name) {
         const statement = `UPDATE category SET name = ? WHERE id = ?;`
         const [result] = await connection.execute(statement,[name,id])
         return result
        
    }
    async remove(id) {
        const statement = `delete from category where id = ?`
        const [result] = await connection.execute(statement,[id])
        return result
        
    }
}

module.exports = new CategoryService()